import {reqUserInfo,reqUserLogin ,reqLogout} from '@/api'
import { setToken,getToken } from '@/untils/token'
const state ={
    code: "",
  token: getToken(),
  userInfo: {},
}
const mutations = {
    USERLOGIN(state,token){
        state.token = token;
    },
    GETUSERINFO(state,userInfo){
        state.userInfo = userInfo;
    },
    LOGOUT(state){
       state.token ='',
       state.userInfo = {}
       localStorage.removeItem('TOKEN');
    }
}
const actions = {
    //退出登录
    async Logout({commit}){
        let result = await reqLogout();
        if(result.code == 200){
            commit('LOGOUT')
        } 
    },

    //登录
    async userLogin({commit},data){
        let result = await reqUserLogin(data);
        console.log('444444444')
        console.log(result)
        if(result.code ==200){
            console.log('555555')
            commit('USERLOGIN',result.data.token);
            setToken(result.data.token);
            return 'ok'
            
        }else{
            return Promise.reject(new Error('faile'))
        }
    },
     //获取用户信息
     async getUserInfo({commit}){
        // let result = await reqUserInfo();
        // if (result.code == 200) {
        //   //提交用户信息
        //   console.log('101010101')
        //   commit("GETUSERINFO", result.data);
        //   return 'ok';
        // }else{
        //   return Promise.reject(new Error('faile'));
        // }
        let result = await reqUserInfo();
        if (result.code == 200) {
          //提交用户信息
          commit("GETUSERINFO", result.data);
          return 'ok';
        }else{
          return Promise.reject(new Error('faile'));
        }
     }
}
const getters ={}
export default {
    state,
    mutations,
    actions,
    getters
}